#include "function.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QImage>
#include <cstring>
#include <cmath>

#define PI 3.14159

//构造函数
Function::Function(QImage image) {
    initialImage = image;
    lastImage = image;
    finalImage = image;
}

//默认构造函数
Function::Function() {

}

//拷贝函数
void Function::copyPicture(QImage image) {
    initialImage = image;
    lastImage = image;
    finalImage = image;
}

//获取原始图片
QImage Function::getInitialImage() {
    return initialImage;
}

//获取上一张图片
QImage Function::getLastImage() {
    return lastImage;
}

//获取最新修改的图片
QImage Function::getFinalImage() {
    return finalImage;
}

//修改上一步修改的图片
void Function::setLastImage(QImage image) {
    lastImage = image;
}

//修改最新图片
void Function::setFinalImage(QImage image) {
    lastImage = finalImage;
    finalImage = image;
}

//图形变换————放大
QImage Function::expandPicture() {
    lastImage = finalImage;

}

//图形变换————缩小
QImage Function::shrinkPicture() {
    lastImage = finalImage;

}

//返回一个最大值
int max(int x, int y) {
    return(x > y ? x : y);
}

//图形变换————旋转
QImage Function::revolvePicture(int a) {
    lastImage = finalImage;

    //将旋转角度转换为弧度
    double angle = a * PI / 180.0;

    //未经旋转的图像的高宽
    int pre_width = finalImage.width();
    int pre_height = finalImage.height();

    //求旋转后图像的高宽
    int sx1 = 0;
    int sy1 = 0;
    int sx2 = 0;
    int sy2 = pre_height;
    int sx3 = pre_width;
    int sy3 = pre_height;
    int sx4 = pre_width;
    int sy4 = 0;
    int aft_width = max(abs(sx4 - sx2), abs(sx3 - sx1)) ;
    int aft_height = max(abs(sy4 - sy2), abs(sy3 - sy1)) ;

    //创建一个新的图像存储修改后的图片
    //QImage *newImage = new QImage(aft_width, aft_height, QImage::Format_RGB32);
    QImage *newImage = new QImage(aft_width, aft_height, QImage::Format_ARGB32);

    //中心点的坐标(带0的为未经旋转的坐标)
    //int midX0 = pre_width / 2;
    //int midY0 = pre_height / 2;

    //计算出一些需要的数据
    double dx = -0.5*aft_width*cos(angle) - 0.5*aft_height*sin(angle) + 0.5*pre_width;
    double dy = 0.5*aft_width*sin(angle) - 0.5*aft_height*cos(angle) + 0.5*pre_width;

    //x0,y0为图像坐标，x,y为数学坐标
    int x0, y0, x, y, t;

    for(int i = 0; i < pre_width; i++) {
        for(int j = 0; j < pre_height; j++) {
            //获取每个点的图像坐标
            x0 = i * 4;
            y0 = j * 4;

            //将图像坐标转换为数学坐标
            x = x0 - pre_width;
            y = y0 - pre_height;

            //套入旋转公式，求得旋转后的数学坐标
            t = x;
            x = x*cos(angle) + y*sin(angle);
            y = y*cos(angle) - t*sin(angle);

            //将数学坐标转换为图像坐标
            x0 = x*cos(angle) + y*sin(angle) + dx;
            y0 = y*cos(angle) - t*sin(angle) + dy;

            unsigned char* pre_pixel = finalImage.bits() + i*3 + j*3;
            unsigned char* aft_pixel = newImage->bits() + x0*3 + y0*3;

            //将像素数据复制过去
            if(x0 >= 0 && x0 < aft_width && y0 >=0 && y0 < aft_height) {

                memcpy(aft_pixel, pre_pixel, 3);
                *(aft_pixel+1) = 255;
            }
        }
    }

    finalImage = *newImage;
    delete newImage;
    return finalImage;
}

//方框滤波
QImage Function::squarePicture() {

}

//中值滤波
QImage Function::midPicture() {

}

//高斯滤波
QImage Function::gaosiPicture() {

}

//双边滤波
QImage Function::sidesPicture() {

}

//均值滤波
QImage Function::averagePicture() {

}

//吸管
QImage Function::strawPicture() {

}

//油漆桶
QImage Function::bucketPicture() {

}

//画笔
QImage Function::paintPicture() {

}

//裁剪
QImage Function::scissorsPicture() {

}

//拼图
QImage Function::jigsawPicture() {

}

//添加文字
QImage Function::textPicture() {

}

